'use strict';

app.controller('loginCtrl', function ($rootScope, $scope,$http, $state) {

    console.log("login");

  
	// 用户未登录状态
    // $scope.$emit("logoutFromChildCtrl", true);

	// 初始化用户信息
	$scope.userData = {
		"name":"",
		"password":"",
		"authority":""
	};

	// 初始化注册信息
	$scope.registerData = {
		"name":"",
		"password":"",
		"passwordagain":"",
		"authority":""
	}

	// 登录事件
	$scope.userLogin = function(){
		// 判断用户名&密码是否为空
		if ($scope.userData.name!=""&&$scope.userData.password!="") {
			// console.log($scope.userData);
			// 判断数据库是否有该用户
			$http.get('/api/getUser',{params:{"USER_NAME":$scope.userData.name}}).then(function(response){
		        // 查询成功
		        if(response.status==200){
		            console.log(response);
		            var dataRes = response.data.user_data;
		            // 用户名不存在
		            if (dataRes == ""){
		            	alert("无此用户！");
		            	$scope.userData = {
							"name":"",
							"password":"",
							"authority":""
						};

		            }
		            // 用户存在且密码正确
		            else if (dataRes != "" && $scope.userData.password == dataRes[0].password) {
		            	// console.log(response);
		            	// alert("正确！")

		            	// 用户登录状态置为true
    					$scope.$emit("logoutFromChildCtrl", true);

    					$scope.$emit("userNameCtrl", $scope.userData);
    					$scope.userData.authority = dataRes[0].authority;
    					
    					$scope.storage.setItem("name",$scope.userData.name);
    					$scope.storage.setItem("password",$scope.userData.password);
    					$scope.storage.setItem("authority",$scope.userData.authority);
    					// console.log($scope.storage.name);
    					// console.log($scope.storage.password);
    					console.log(window.localStorage);
    					console.log($scope.storage);

    					$state.go('app.actual');

		            	// window.location.href = "http://localhost:3000/#/app/actual";
		            	//如果权限是管理员则进入可以修改阈值的界面
		            	// if($scope.userData.authority==0){
		            	// 	$state.go('app.actual');
		            	// }
		            	//如果权限为普通用户，则看不到修改阈值的界面
		            	// else{
		            	// 	
		            	// }
		            	

		            } 
		            // 用户密码不正确
		            else {
		            	// debugger;
		            	alert("密码错误！");
		            	$scope.userData.password = "";

		            }
		            
		            


		      
		        }
		        // 查询错误
		        else{
		            console.log(response.status);
		        }
		    });

		} 
		else {
			alert("用户名或密码不能为空！")
		}
	}

  //   $scope.$watch('$scope.userData',function(){
		// console.log($scope.userData);

  //   });
	// 注册事件
	$scope.userRegister = function(){
		console.log($scope.registerData);
		// 判断数据库是否有该用户
		$http.get('/api/getUser',{params:{"USER_NAME":$scope.registerData.name}}).then(function(response){
	        // 查询成功
	        if(response.status==200){
	            console.log(response);
	            var dataRes = response.data.user_data;
	            // 用户不存在
	            if (dataRes == "") {
	            	// 注册新用户
	            	$http.get('/api/addUser',{params:{"USER_NAME":$scope.registerData.name,"USER_PASSWORD":$scope.registerData.password,"USER_AUTHORITY":$scope.registerData.authority}}).then(function(response){
				        
				        if(response.status==200){
				            console.log(response);
				            // window.location.href = "http://localhost:3000/#/login";
				            $state.go('login');
				      
				        }
				        else{
				            console.log(response.status);
				            // alert("用户不存在！");
				        }
				    });

	            } 
	            // 用户已存在
	            else {
	            	alert("用户已存在！");
	            	$scope.registerData = {
						"name":"",
						"password":"",
						"passwordagain":"",
						"authority":""
					}
	            }
	            
	            
	            


	      
	        }
	        // 查询错误
	        else{
	            console.log(response.status);
	            // alert("用户不存在！");
	        }
	    });
		



	}

    


    // 用户未登录状态
    // $scope.$emit("logoutFromChildCtrl", true);
    
});